$_ or @_ in subrountine?

E

ela

I pass a long file name to a subrountine by:

my ($file1, $file2, $file3) = @ARGV;

&mysub($file1);

sub mysub {
my $subfile1 = $_; #this doesn't work, $subfile1 remains to be ""
my ($subfile2) = @_; #this works
}

why does such a problem happen?
 
K

Keith Keller

I pass a long file name to a subrountine by:

my ($file1, $file2, $file3) = @ARGV;

&mysub($file1);

Don't use & to call subroutines unless you know why you need it.
sub mysub {
my $subfile1 = $_; #this doesn't work, $subfile1 remains to be ""
my ($subfile2) = @_; #this works
}

why does such a problem happen?

Because $_ is unrelated to @_. Read perldoc perlvar to find out what
those variables actually are. (Since @_ is an array, $_[0] is the first
element of the array. But it's still not related to $_.)

--keith
 
J

Jürgen Exner

ela said:
I pass a long file name to a subrountine by:

my ($file1, $file2, $file3) = @ARGV;

&mysub($file1);

Why are you overriding the prototype of mysub() when you are not even
declaring a prototype for mysub()? That doesn't make any sense.
sub mysub {
my $subfile1 = $_; #this doesn't work, $subfile1 remains to be ""

Really? That surprises me. I would have assumed it remained undef.
my ($subfile2) = @_; #this works
}
why does such a problem happen?

There is no problem. $_ and @_ are two completely different variables
which have nothing to do with each other, just like $foo and @foo are
two different variables.

jue
 
R

Rainer Weikusat

Jürgen Exner said:
Why are you overriding the prototype of mysub() when you are not even
declaring a prototype for mysub()? That doesn't make any sense.


Really? That surprises me. I would have assumed it remained undef.

$_ is a 'special global variable' and this means that the assignment
quoted above will assign the present value of $_ to $subfile. And this
can pretty much be anything, since $_ is used as 'default variable' in
various constructs, such as

for (@a) { ... }

while (<>) { ... }

given ($blah) {
when (/something) {
}
}

Of course, Perl still does automatic type conversion whenever that
seems to be what was desired,

[rw@sapphire]~ $perl -e 'print undef eq "", "\n"'
1

and undefined values still stringify to empty strings. No matter how
ideological impure this may seem to whomever.
 
R

Randal L. Schwartz

ela> &mysub($file1);

ela> sub mysub {
ela> my $subfile1 = $_; #this doesn't work, $subfile1 remains to be ""
ela> my ($subfile2) = @_; #this works
ela> }

ela> why does such a problem happen?

Perhaps you were looking for:

my $subfile1 = $_[0];

Note that $_ and $_[0] (part of @_) are separate variables.

print "Just another Perl hacker,"; # the original
 
J

Jürgen Exner

Rainer Weikusat said:
$_ is a 'special global variable' and this means that the assignment
quoted above will assign the present value of $_ to $subfile. And this
can pretty much be anything, since $_ is used as 'default variable' in
various constructs, such as

for (@a) { ... }

while (<>) { ... }

given ($blah) {
when (/something) {
}
}

Sure. But none of these possibilities appear in the OPs program.
and undefined values still stringify to empty strings. No matter how
ideological impure this may seem to whomever.

Sure. But the OP explicitely said the value was the empty string, he
didn't talk about the stringified value.

jue
 
U

Uri Guttman

JE> Sure. But the OP explicitely said the value was the empty string, he
JE> didn't talk about the stringified value.

remember, our dear rainman doesn't get the difference of undef from
''. i wonder if he ever uses defined for anything? he might as well just
eq to ''.

uri
 
D

Dr.Ruud


Not quite.

In case of the for ( .. ), the $_ is localized, like in:

perl -Mstrict -wle '
$_ = q{foo};
print;
print for map " $_", qw( bar baz );
print;
'
foo
bar
baz
foo
 
R

Rainer Weikusat

Uri Guttman said:
JE> Sure. But the OP explicitely said the value was the empty string, he
JE> didn't talk about the stringified value.

remember, our dear rainman doesn't get the difference of undef from
''. i wonder if he ever uses defined for anything? he might as well just
eq to ''.
[...]

------------ Perl Developer Recruiting and Placement Services -------------

Given that you apparently don't have an interest in anything except
badmouthing others based on statements about them you are making up as
it suits you, I really wonder who placed you in a position where you
have any responsibilty for anything or any authority over anyone
because if I've encountered someone who seemed to be completely unfit
for tasks like that, that would be you. You're decidely worse than my
ex-superiors in the navy who were men enough to accept that people
could obey to orders while still having their own thoughts about them.
 
R

Rainer Weikusat

Tad McClellan said:
So far, so good.



No, it must be undef in the program that is being discussed in this
thread.

Maybe, maybe not. There was no context hinting at that in the posting
I replied to and

it is a special case in this respect: The strict module will not bark
at it. Yet accidental use of it may yield anything, depending on
whatever the context of the use happened to be. And I doubt that the
OP was aware of that since he used it in a place where $_[0] should
have been used instead. Hence, it seemed worth mentioning its special
properties.
 
R

Rainer Weikusat

Jürgen Exner said:
[...]
[...]
undefined values still stringify to empty strings. No matter how
ideological impure this may seem to whomever.

Sure. But the OP explicitely said the value was the empty string, he
didn't talk about the stringified value.

The Perl documentation calls this 'an undefined empty string' in
various places. I quoted a couple of them not that long ago.
 
E

ela

Tad McClellan said:
I pointed this out to you a couple of years ago, yet here
you are still doing it...

Sorry for my late reply because when the first reply had appeared, I
immediately checked the reference and fixed the error. Again, I wish to
emphasize that I'm poor at organizing knowledge learnt and not intentively
ignore your kind advice. I'm still struggling to better myself and I hope I
can be one day fully qualified as a poster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top