$_ or @_ in subrountine?

Discussion in 'Perl Misc' started by ela, Sep 16, 2011.

  1. ela

    ela Guest

    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?
    ela, Sep 16, 2011
    #1
    1. Advertising

  2. ela

    Keith Keller Guest

    On 2011-09-16, ela <> wrote:
    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
    Keith Keller, Sep 16, 2011
    #2
    1. Advertising

  3. "ela" <> wrote:
    >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
    Jürgen Exner, Sep 16, 2011
    #3
  4. Jürgen Exner <> writes:
    > "ela" <> wrote:
    >>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.


    $_ 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.
    Rainer Weikusat, Sep 16, 2011
    #4
  5. >>>>> "ela" == ela <> writes:

    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

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    Randal L. Schwartz, Sep 16, 2011
    #5
  6. Rainer Weikusat <> wrote:
    >Jürgen Exner <> writes:
    >> "ela" <> wrote:
    >>>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.

    >
    >$_ 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
    Jürgen Exner, Sep 17, 2011
    #6
  7. ela

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> Rainer Weikusat <> wrote:

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


    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

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------
    Uri Guttman, Sep 17, 2011
    #7
  8. ela

    Dr.Ruud Guest

    On 2011-09-17 01:43, Jürgen Exner wrote:
    > Rainer Weikusat<> wrote:


    >> $_ 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.


    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

    --
    Ruud
    Dr.Ruud, Sep 17, 2011
    #8
  9. "Uri Guttman" <> writes:
    >>>>>> "JE" == Jürgen Exner <> writes:

    >
    > JE> Rainer Weikusat <> wrote:
    >
    > >> and undefined values still stringify to empty strings. No matter how
    > >> ideological impure this may seem to whomever.

    >
    > 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.
    Rainer Weikusat, Sep 18, 2011
    #9
  10. Tad McClellan <> writes:
    > Rainer Weikusat <> wrote:
    >> Jürgen Exner <> writes:
    >>> "ela" <> wrote:
    >>>>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.

    >>
    >> $_ is a 'special global variable' and this means that the assignment
    >> quoted above will assign the present value of $_ to $subfile.

    >
    >
    > So far, so good.
    >
    >
    >> And this
    >> can pretty much be anything,

    >
    > 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

    >> since $_ is used as 'default variable' in
    >> various constructs, such as
    >>
    >> for (@a) { ... }
    >>
    >> while (<>) { ... }
    >>
    >> given ($blah) {
    >> when (/something) {
    >> }
    >> }


    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.
    Rainer Weikusat, Sep 18, 2011
    #10
  11. Jürgen Exner <> writes:
    > Rainer Weikusat <> wrote:
    >>Jürgen Exner <> writes:
    >>> "ela" <> wrote:


    [...]

    >>>>sub mysub {
    >>>> my $subfile1 = $_; #this doesn't work, $subfile1 remains to be ""
    >>>
    >>> Really? That surprises me. I would have assumed it remained undef.


    [...]

    >> 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.
    Rainer Weikusat, Sep 18, 2011
    #11
  12. ela

    ela Guest

    "Tad McClellan" <> wrote in message
    > 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.
    ela, Sep 30, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page