Magic $a $b

Discussion in 'Perl Misc' started by Achim Peters, Feb 28, 2008.

  1. Achim Peters

    Achim Peters Guest

    Hi,

    I'm a noob to perl and a frequent reader of this group. Whenever anyone
    in here uses $a and/or $b in a non-sort context s/he gets a "Don't!
    They're magic." as a response. Now, I believe in magic and the wisdom of
    regulars and that's why I mentioned this general rule in
    de.comp.lang.perl.misc.

    But there happened to be a non-believer and he dared me with "Oh,
    really? Perl this:"

    use strict;
    use warnings;

    $a = 4711;

    my @arr = qw /x a u q r/;
    @arr = sort { $a cmp $b } @arr;

    print "$a\n";

    which in fact does print the 4711 (a German "magic" number) with Perl
    5.8.2, thus no side effects of the sorting (except of course that the
    missing "my" in front of $a does not cause an compile error.

    Is there any example to prove his impertinence of questioning the gurus'
    wisdom?

    TIA

    Bye
    Achim
     
    Achim Peters, Feb 28, 2008
    #1
    1. Advertising

  2. Achim Peters <> writes:

    > But there happened to be a non-believer and he dared me with "Oh,
    > really? Perl this:"
    >
    > use strict;
    > use warnings;
    >
    > $a = 4711;


    See mom, no warning! - That's magic.

    Using $a and $b defies parts of of using strict.

    //Makholm
     
    Peter Makholm, Feb 28, 2008
    #2
    1. Advertising

  3. Achim Peters <> writes:

    > which in fact does print the 4711 (a German "magic" number) with Perl
    > 5.8.2, thus no side effects of the sorting (except of course that the
    > missing "my" in front of $a does not cause an compile error.
    >
    > Is there any example to prove his impertinence of questioning the gurus'
    > wisdom?


    I would think:

    sub srt {
    my @arr = qw /x a u q r/;
    @arr = sort { $a cmp $b } @arr;
    }

    srt();
    my $a = 4711;
    print "$a\n";

    versus:

    srt();
    my $a = 4711;
    print "$a\n";

    sub srt {
    my @arr = qw /x a u q r/;
    @arr = sort { $a cmp $b } @arr;
    }

    would be enough reason not to use $a and $b.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Feb 28, 2008
    #3
  4. Achim Peters

    Achim Peters Guest

    Joost Diepenmaat schrieb:
    > Achim Peters <> writes:
    >
    >> which in fact does print the 4711 (a German "magic" number) with Perl
    >> 5.8.2, thus no side effects of the sorting (except of course that the
    >> missing "my" in front of $a does not cause an compile error.
    >>
    >> Is there any example to prove his impertinence of questioning the gurus'
    >> wisdom?

    >
    > I would think:


    > my $a = 4711;

    [...]

    > would be enough reason not to use $a and $b.


    Tnx! He converted and now is a true fellow believer. ;-)

    Bye
    Achim
     
    Achim Peters, Feb 29, 2008
    #4
  5. Achim Peters

    szr Guest

    Joost Diepenmaat wrote:
    > Achim Peters <> writes:
    >
    >> which in fact does print the 4711 (a German "magic" number) with Perl
    >> 5.8.2, thus no side effects of the sorting (except of course that the
    >> missing "my" in front of $a does not cause an compile error.
    >>
    >> Is there any example to prove his impertinence of questioning the
    >> gurus' wisdom?

    >
    > I would think:
    >
    > sub srt {
    > my @arr = qw /x a u q r/;
    > @arr = sort { $a cmp $b } @arr;
    > }
    >
    > srt();
    > my $a = 4711;
    > print "$a\n";
    >
    > versus:
    >
    > srt();
    > my $a = 4711;
    > print "$a\n";
    >
    > sub srt {
    > my @arr = qw /x a u q r/;
    > @arr = sort { $a cmp $b } @arr;
    > }
    >
    > would be enough reason not to use $a and $b.


    When I try the latter, I get the following, using Perl 5.8.8:

    Can't use "my $a" in sort comparison at line 9.

    0001: #!/usr/local/bin/perl -w
    0002: my @arr = srt();
    0003: my $a = 4711;
    0004: print "$a\n";
    0005: print join (', ', @arr);
    0006:
    0007: sub srt {
    0008: my @arr = qw /x a u q r/;
    0009: @arr = sort { $a cmp $b } @arr;
    0010: }


    The error message itself seems to be wrong, or more percisely, the wrong
    error for the given situation. There is no "my" in the "sort { ... }"
    clause. Is this be a bug?

    Furthur, why does it generate an error in the first place?

    --
    szr
     
    szr, Feb 29, 2008
    #5
  6. Achim Peters

    J. Gleixner Guest

    szr wrote:
    > Joost Diepenmaat wrote:
    >> Achim Peters <> writes:
    >>
    >>> which in fact does print the 4711 (a German "magic" number) with Perl
    >>> 5.8.2, thus no side effects of the sorting (except of course that the
    >>> missing "my" in front of $a does not cause an compile error.
    >>>
    >>> Is there any example to prove his impertinence of questioning the
    >>> gurus' wisdom?

    >> I would think:
    >>
    >> sub srt {
    >> my @arr = qw /x a u q r/;
    >> @arr = sort { $a cmp $b } @arr;
    >> }
    >>
    >> srt();
    >> my $a = 4711;
    >> print "$a\n";
    >>
    >> versus:
    >>
    >> srt();
    >> my $a = 4711;
    >> print "$a\n";
    >>
    >> sub srt {
    >> my @arr = qw /x a u q r/;
    >> @arr = sort { $a cmp $b } @arr;
    >> }
    >>
    >> would be enough reason not to use $a and $b.

    >
    > When I try the latter, I get the following, using Perl 5.8.8:
    >
    > Can't use "my $a" in sort comparison at line 9.
    >
    > 0001: #!/usr/local/bin/perl -w
    > 0002: my @arr = srt();
    > 0003: my $a = 4711;
    > 0004: print "$a\n";
    > 0005: print join (', ', @arr);
    > 0006:
    > 0007: sub srt {
    > 0008: my @arr = qw /x a u q r/;
    > 0009: @arr = sort { $a cmp $b } @arr;
    > 0010: }
    >
    >
    > The error message itself seems to be wrong, or more percisely, the wrong
    > error for the given situation. There is no "my" in the "sort { ... }"
    > clause. Is this be a bug?
    >
    > Furthur, why does it generate an error in the first place?


    Are you kidding??????????

    You're supposed to get an error. That's the reason Joost
    posted the very concise example - to show the OP why
    it might be bad to use $a.
     
    J. Gleixner, Feb 29, 2008
    #6
  7. Achim Peters

    Ben Morrow Guest

    Quoth "szr" <>:
    > Joost Diepenmaat wrote:
    > >
    > > I would think:

    <snip>
    > >
    > > srt();
    > > my $a = 4711;
    > > print "$a\n";
    > >
    > > sub srt {
    > > my @arr = qw /x a u q r/;
    > > @arr = sort { $a cmp $b } @arr;
    > > }
    > >
    > > would be enough reason not to use $a and $b.

    >
    > When I try the latter, I get the following, using Perl 5.8.8:
    >
    > Can't use "my $a" in sort comparison at line 9.
    >
    > The error message itself seems to be wrong, or more percisely, the wrong
    > error for the given situation. There is no "my" in the "sort { ... }"
    > clause. Is this be a bug?


    No, it's not. The point is that the $a in scope at line 9 is the 'my $a'
    from five lines earlier, rather that the global $main::a in scope at the
    start of the program, and sort (for various nasty implementation-specfic
    reasons) can't use a lexical $a. This is exactly why you shouldn't use
    $a other than for sort.

    Ben
     
    Ben Morrow, Feb 29, 2008
    #7
  8. Achim Peters

    szr Guest

    Ben Morrow wrote:
    > Quoth "szr" <>:
    >> Joost Diepenmaat wrote:
    >>>
    >>> I would think:

    > <snip>
    >>>
    >>> srt();
    >>> my $a = 4711;
    >>> print "$a\n";
    >>>
    >>> sub srt {
    >>> my @arr = qw /x a u q r/;
    >>> @arr = sort { $a cmp $b } @arr;
    >>> }
    >>>
    >>> would be enough reason not to use $a and $b.

    >>
    >> When I try the latter, I get the following, using Perl 5.8.8:
    >>
    >> Can't use "my $a" in sort comparison at line 9.
    >>
    >> The error message itself seems to be wrong, or more percisely, the
    >> wrong error for the given situation. There is no "my" in the "sort {
    >> ... }" clause. Is this be a bug?

    >
    > No, it's not. The point is that the $a in scope at line 9 is the 'my
    > $a' from five lines earlier, rather that the global $main::a in scope
    > at the start of the program, and sort (for various nasty
    > implementation-specfic reasons) can't use a lexical $a. This is
    > exactly why you shouldn't use $a other than for sort.


    Ah. Its very clear now. Thank you (and J. Gleixner) I'm nto sure why I
    didn't catch that.

    --
    szr
     
    szr, Mar 1, 2008
    #8
    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.
Similar Threads
  1. Nilsson Mats

    Wrapper on magic line?

    Nilsson Mats, Dec 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    645
    Nilsson Mats
    Dec 9, 2003
  2. Martin

    Application name magic

    Martin, Jan 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    340
    Martin
    Jan 28, 2004
  3. =?Utf-8?B?U2hhd24=?=

    Magic Trick Datagrid Delete Row

    =?Utf-8?B?U2hhd24=?=, Apr 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,502
    =?Utf-8?B?U2hhd24=?=
    Apr 6, 2005
  4. lawrence

    bad magic number

    lawrence, Sep 20, 2003, in forum: Java
    Replies:
    5
    Views:
    850
    Roedy Green
    Oct 10, 2003
  5. Giles Bowkett
    Replies:
    9
    Views:
    429
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page