FAQ
Hi all,

I've got a script that read in a HTML template file to an array, and then
write it out a number of times substituting part of the text.

Can anyone tell me why the following code fragment works

__BEGIN__
my $line='';
print $line while ($line=shift @HTML)!~/^<!--break/; # copy until break
do {} until (shift @HTML)=~/^-->/; # skip to end of comment
print "blah\n"; # insert replacement code

print "$_" foreach (@HTML); # copy rest of file
__END__

while this one, using $_ instead of $line fails stating once per line before
the break that I'm using an uninitialised variable on the 'while' line

__BEGIN__
print while (shift @HTML)!~/^<!--break/; # copy until break
do {} until (shift @HTML)=~/^-->/; # skip to end of comment
print "blah\n"; # insert replacement code

print "$_" foreach (@HTML); # copy rest of file
__END__
--
Gary Stainburn

This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

Search Discussions

  • Felix Geerinckx at Apr 18, 2002 at 12:52 pm
    on Thu, 18 Apr 2002 13:26:21 GMT, gary.stainburn@ringways.co.uk (Gary
    Stainburn) wrote:
    [..]
    while this one, using $_ instead of $line fails stating once per line
    before the break that I'm using an uninitialised variable on the
    'while' line

    __BEGIN__
    print while (shift @HTML)!~/^<!--break/; # copy until break
    Your assume that '$_' is assigned to the shifted '@HTML' array-element
    here, but it isn't. You should use

    print while ($_ = shift @HTML) !~ etc...

    You get the uninitialized value warning because you use 'print' without
    parameters, so it defaults to '$_' which isn't initialized yet.

    --
    felix
  • Gary Stainburn at Apr 18, 2002 at 1:11 pm

    On Thursday 18 April 2002 1:52 pm, Felix Geerinckx wrote:
    on Thu, 18 Apr 2002 13:26:21 GMT, gary.stainburn@ringways.co.uk (Gary

    Stainburn) wrote:
    [..]
    while this one, using $_ instead of $line fails stating once per line
    before the break that I'm using an uninitialised variable on the
    'while' line

    __BEGIN__
    print while (shift @HTML)!~/^<!--break/; # copy until break
    Your assume that '$_' is assigned to the shifted '@HTML' array-element
    here, but it isn't. You should use

    print while ($_ = shift @HTML) !~ etc...

    You get the uninitialized value warning because you use 'print' without
    parameters, so it defaults to '$_' which isn't initialized yet.
    Thanks for that Felix.

    While your answer makes sense, why doesn't shift use the $_ as I obviously
    expected?

    --
    Gary Stainburn

    This email does not contain private or confidential material as it
    may be snooped on by interested government parties for unknown
    and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
  • Felix Geerinckx at Apr 18, 2002 at 1:36 pm
    on Thu, 18 Apr 2002 14:09:57 GMT, gary.stainburn@ringways.co.uk (Gary
    Stainburn) wrote:
    While your answer makes sense, why doesn't shift use the $_ as I
    obviously expected?
    If you

    perldoc perlvar

    and look for the description of '$_', you will see where it is 'assumed'.

    To complicate matters, the 'shift' operator has it's own default argument,
    which is '@_', since 'shift' needs an array as parameter.

    --
    felix

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 18, '02 at 12:28p
activeApr 18, '02 at 1:36p
posts4
users2
websiteperl.org

2 users in discussion

Gary Stainburn: 2 posts Felix Geerinckx: 2 posts

People

Translate

site design / logo © 2022 Grokbase