on Code references

Discussion in 'Perl Misc' started by sharma__r@hotmail.com, Jan 21, 2009.

  1. Guest

    Hi Perl gurus,

    I am having trouble with calling subroutines using a reference
    variable.

    #!/usr/local/bin/perl
    use 5.8.0;
    use strict;
    use warnings;

    # anonymous subroutine stored in a reference variable
    my $reduce_ref = sub (&@) {
    my $code = shift;
    no strict 'refs';

    return shift unless @_ > 1;

    use vars qw($a $b);

    my $caller = caller;
    local(*{$caller."::a"}) = \my $a;
    local(*{$caller."::b"}) = \my $b;

    $a = shift;
    foreach (@_) {
    $b = $_;
    $a = &{$code}();
    }

    $a;
    };

    my @A = qw( 3 54 22);

    ## to compute the max of array
    print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    __END__

    ## this gives the following error:
    # Use of uninitialized value in numeric gt (>) at ...
    # Odd number of elements in anonymous hash at ...
    # Use of uninitialized value in anonymous hash ({}) at ...
    # Not a CODE reference at ...


    Had I used a named subroutine then the things work fine.
    What could be the problem here?

    with thanks,
     
    , Jan 21, 2009
    #1
    1. Advertising

  2. rthangam Guest

    On Jan 21, 1:13 pm, wrote:
    > Hi Perl gurus,
    >
    > I am having trouble with calling subroutines using a reference
    > variable.
    >
    > #!/usr/local/bin/perl
    > use 5.8.0;
    > use strict;
    > use warnings;
    >
    > # anonymous subroutine stored in a reference variable
    > my $reduce_ref = sub (&@) {
    >         my $code = shift;
    >         no strict 'refs';
    >
    >         return shift unless @_ > 1;
    >
    >         use vars qw($a $b);
    >
    >         my $caller = caller;
    >         local(*{$caller."::a"}) = \my $a;
    >         local(*{$caller."::b"}) = \my $b;
    >
    >         $a = shift;
    >         foreach (@_) {
    >                 $b = $_;
    >                 $a = &{$code}();
    >         }
    >
    >         $a;
    >
    > };
    >
    > my @A = qw( 3 54 22);
    >
    > ## to compute the max of array
    > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > __END__
    >
    > ## this gives the following error:
    > # Use of uninitialized value in numeric gt (>) at ...
    > # Odd number of elements in anonymous hash at ...
    > # Use of uninitialized value in anonymous hash ({}) at ...
    > # Not a CODE reference at ...
    >
    > Had I used a named subroutine then the things work fine.
    > What could be the problem here?
    >
    > with thanks,


    you can use eval !! that should solve the problem:

    #!/usr/local/bin/perl
    use 5.8.0;
    use strict;
    use warnings;

    use Data::Dumper;

    # anonymous subroutine stored in a reference variable
    my $reduce_ref = sub (&@) {
    my $code = shift;
    no strict 'refs';

    return shift unless @_ > 1;

    use vars qw($a $b);

    my $caller = caller;
    local(*{$caller."::a"}) = \my $a;
    local(*{$caller."::b"}) = \my $b;

    $a = shift;
    foreach (@_) {
    $b = $_;
    $a = eval $code;
    }

    $a;

    };

    my @A = qw( 3 54 22);

    ## to compute the max of array
    print $reduce_ref->( '($a > $b) ? $a : $b', @A);

    __END__


    Please check it
     
    rthangam, Jan 21, 2009
    #2
    1. Advertising

  3. rthangam Guest

    On Jan 21, 1:13 pm, wrote:
    > Hi Perl gurus,
    >
    > I am having trouble with calling subroutines using a reference
    > variable.
    >
    > #!/usr/local/bin/perl
    > use 5.8.0;
    > use strict;
    > use warnings;
    >
    > # anonymous subroutine stored in a reference variable
    > my $reduce_ref = sub (&@) {
    >         my $code = shift;
    >         no strict 'refs';
    >
    >         return shift unless @_ > 1;
    >
    >         use vars qw($a $b);
    >
    >         my $caller = caller;
    >         local(*{$caller."::a"}) = \my $a;
    >         local(*{$caller."::b"}) = \my $b;
    >
    >         $a = shift;
    >         foreach (@_) {
    >                 $b = $_;
    >                 $a = &{$code}();
    >         }
    >
    >         $a;
    >
    > };
    >
    > my @A = qw( 3 54 22);
    >
    > ## to compute the max of array
    > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > __END__
    >
    > ## this gives the following error:
    > # Use of uninitialized value in numeric gt (>) at ...
    > # Odd number of elements in anonymous hash at ...
    > # Use of uninitialized value in anonymous hash ({}) at ...
    > # Not a CODE reference at ...
    >
    > Had I used a named subroutine then the things work fine.
    > What could be the problem here?
    >
    > with thanks,


    You can use eval

    #!/usr/local/bin/perl
    use 5.8.0;
    use strict;
    use warnings;

    use Data::Dumper;

    # anonymous subroutine stored in a reference variable
    my $reduce_ref = sub (&@) {
    my $code = shift;
    no strict 'refs';

    return shift unless @_ > 1;

    use vars qw($a $b);

    my $caller = caller;
    local(*{$caller."::a"}) = \my $a;
    local(*{$caller."::b"}) = \my $b;

    $a = shift;
    foreach (@_) {
    $b = $_;
    $a = eval $code;
    }

    $a;

    };



    my @A = qw( 3 54 22 12 100);

    ## to compute the max of array
    print $reduce_ref->( '($a > $b) ? $a : $b', @A);

    __END__
     
    rthangam, Jan 21, 2009
    #3
  4. Guest

    On Jan 21, 2:56 pm, rthangam <> wrote:
    > On Jan 21, 1:13 pm, wrote:
    >
    >
    >
    > > Hi Perl gurus,

    >
    > > I am having trouble with calling subroutines using a reference
    > > variable.

    >
    > > #!/usr/local/bin/perl
    > > use 5.8.0;
    > > use strict;
    > > use warnings;

    >
    > > # anonymous subroutine stored in a reference variable
    > > my $reduce_ref = sub (&@) {
    > > my $code = shift;
    > > no strict 'refs';

    >
    > > return shift unless @_ > 1;

    >
    > > use vars qw($a $b);

    >
    > > my $caller = caller;
    > > local(*{$caller."::a"}) = \my $a;
    > > local(*{$caller."::b"}) = \my $b;

    >
    > > $a = shift;
    > > foreach (@_) {
    > > $b = $_;
    > > $a = &{$code}();
    > > }

    >
    > > $a;

    >
    > > };

    >
    > > my @A = qw( 3 54 22);

    >
    > > ## to compute the max of array
    > > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > > __END__

    >
    > > ## this gives the following error:
    > > # Use of uninitialized value in numeric gt (>) at ...
    > > # Odd number of elements in anonymous hash at ...
    > > # Use of uninitialized value in anonymous hash ({}) at ...
    > > # Not a CODE reference at ...

    >
    > > Had I used a named subroutine then the things work fine.
    > > What could be the problem here?

    >
    > > with thanks,

    >
    > You can use eval
    >
    > #!/usr/local/bin/perl
    > use 5.8.0;
    > use strict;
    > use warnings;
    >
    > use Data::Dumper;
    >
    > # anonymous subroutine stored in a reference variable
    > my $reduce_ref = sub (&@) {
    > my $code = shift;
    > no strict 'refs';
    >
    > return shift unless @_ > 1;
    >
    > use vars qw($a $b);
    >
    > my $caller = caller;
    > local(*{$caller."::a"}) = \my $a;
    > local(*{$caller."::b"}) = \my $b;
    >
    > $a = shift;
    > foreach (@_) {
    > $b = $_;
    > $a = eval $code;
    > }
    >
    > $a;
    >
    > };
    >
    > my @A = qw( 3 54 22 12 100);
    >
    > ## to compute the max of array
    > print $reduce_ref->( '($a > $b) ? $a : $b', @A);
    >
    > __END__



    It works! But is possible to make it work without 'eval'?
     
    , Jan 21, 2009
    #4
  5. wrote in news:0a2d735d-066f-4f45-98b8-
    :

    > I am having trouble with calling subroutines using a reference
    > variable.
    >
    > #!/usr/local/bin/perl
    > use 5.8.0;
    > use strict;
    > use warnings;
    >
    > # anonymous subroutine stored in a reference variable
    > my $reduce_ref = sub (&@) {
    > my $code = shift;
    > no strict 'refs';
    >
    > return shift unless @_ > 1;
    >
    > use vars qw($a $b);
    >
    > my $caller = caller;
    > local(*{$caller."::a"}) = \my $a;
    > local(*{$caller."::b"}) = \my $b;
    >
    > $a = shift;
    > foreach (@_) {
    > $b = $_;
    > $a = &{$code}();
    > }
    >
    > $a;
    > };
    >
    > my @A = qw( 3 54 22);
    >
    > ## to compute the max of array
    > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > __END__
    >
    > ## this gives the following error:
    > # Use of uninitialized value in numeric gt (>) at ...
    > # Odd number of elements in anonymous hash at ...
    > # Use of uninitialized value in anonymous hash ({}) at ...
    > # Not a CODE reference at ...


    Funny, it gives me:

    C:\Temp> ttt
    Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
    (Missing operator before ?)
    syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
    \ttt.pl aborted due to compilation errors.

    If I correct the last line:

    print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);

    Then the script outputs 54.

    The simple resolution of the problem aside, I am not sure why you want
    to do this when you can use:

    http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Jan 21, 2009
    #5
  6. Guest

    On Jan 21, 6:22 pm, "A. Sinan Unur" <> wrote:
    > wrote in news:0a2d735d-066f-4f45-98b8-
    > :
    >
    >
    >
    > > I am having trouble with calling subroutines using a reference
    > > variable.

    >
    > > #!/usr/local/bin/perl
    > > use 5.8.0;
    > > use strict;
    > > use warnings;

    >
    > > # anonymous subroutine stored in a reference variable
    > > my $reduce_ref = sub (&@) {
    > >      my $code = shift;
    > >      no strict 'refs';

    >
    > >      return shift unless @_ > 1;

    >
    > >      use vars qw($a $b);

    >
    > >      my $caller = caller;
    > >      local(*{$caller."::a"}) = \my $a;
    > >      local(*{$caller."::b"}) = \my $b;

    >
    > >      $a = shift;
    > >      foreach (@_) {
    > >           $b = $_;
    > >           $a = &{$code}();
    > >      }

    >
    > >      $a;
    > > };

    >
    > > my @A = qw( 3 54 22);

    >
    > > ## to compute the max of array
    > > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > > __END__

    >
    > > ## this gives the following error:
    > > # Use of uninitialized value in numeric gt (>) at ...
    > > # Odd number of elements in anonymous hash at ...
    > > # Use of uninitialized value in anonymous hash ({}) at ...
    > > # Not a CODE reference at ...

    >
    > Funny, it gives me:
    >
    > C:\Temp> ttt
    > Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
    >         (Missing operator before  ?)
    > syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
    > \ttt.pl aborted due to compilation errors.
    >
    > If I correct the last line:
    >
    > print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);
    >
    > Then the script outputs 54.
    >
    > The simple resolution of the problem aside, I am not sure why you want
    > to do this when you can use:
    >
    > http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm
    >
    > Sinan
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/



    Thanks for resolving that!

    The error messages are differing maybe because of the OS differences.
    I ran on Sun-Solaris Unix.

    I am using the code from List::MoreUtils module, but was trying to use
    it
    as a code reference rather than a named subroutine.

    Regards,
     
    , Jan 21, 2009
    #6
  7. <> wrote:
    > On Jan 21, 6:22 pm, "A. Sinan Unur" <> wrote:


    >> Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
    >>         (Missing operator before  ?)
    >> syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
    >> \ttt.pl aborted due to compilation errors.



    > The error messages are differing maybe because of the OS differences.



    It is a syntax error.

    The syntax of Perl does not depend on the OS.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 21, 2009
    #7
  8. C.DeRykus Guest

    On Jan 21, 6:30 am, wrote:
    > On Jan 21, 6:22 pm, "A. Sinan Unur" <> wrote:
    >
    >
    >
    > > wrote in news:0a2d735d-066f-4f45-98b8-
    > > :

    >
    > > > I am having trouble with calling subroutines using a reference
    > > > variable.

    >
    > > > #!/usr/local/bin/perl
    > > > use 5.8.0;
    > > > use strict;
    > > > use warnings;

    >
    > > > # anonymous subroutine stored in a reference variable
    > > > my $reduce_ref = sub (&@) {
    > > > my $code = shift;
    > > > no strict 'refs';

    >
    > > > return shift unless @_ > 1;

    >
    > > > use vars qw($a $b);

    >
    > > > my $caller = caller;
    > > > local(*{$caller."::a"}) = \my $a;
    > > > local(*{$caller."::b"}) = \my $b;

    >
    > > > $a = shift;
    > > > foreach (@_) {
    > > > $b = $_;
    > > > $a = &{$code}();
    > > > }

    >
    > > > $a;
    > > > };

    >
    > > > my @A = qw( 3 54 22);

    >
    > > > ## to compute the max of array
    > > > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
    > > > __END__

    >
    > > > ## this gives the following error:
    > > > # Use of uninitialized value in numeric gt (>) at ...
    > > > # Odd number of elements in anonymous hash at ...
    > > > # Use of uninitialized value in anonymous hash ({}) at ...
    > > > # Not a CODE reference at ...

    >
    > > Funny, it gives me:

    >
    > > C:\Temp> ttt
    > > Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
    > > (Missing operator before ?)
    > > syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
    > > \ttt.pl aborted due to compilation errors.

    >
    > > If I correct the last line:

    >
    > > print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);

    >
    > > Then the script outputs 54.

    >
    > > The simple resolution of the problem aside, I am not sure why you want
    > > to do this when you can use:

    >
    > >http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm

    >
    > > Sinan
    > > --
    > > A. Sinan Unur <>
    > > (remove .invalid and reverse each component for email address)

    >
    > > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/

    >
    > Thanks for resolving that!
    >
    > The error messages are differing maybe because of the OS differences.
    > I ran on Sun-Solaris Unix.
    >
    > I am using the code from List::MoreUtils module, but was trying to use
    > it
    > as a code reference rather than a named subroutine.


    s/List::MoreUtils/List::Util/

    --
    Charles DeRykus
     
    C.DeRykus, Jan 21, 2009
    #8
  9. On 2009-01-21, A. Sinan Unur <> wrote:
    > wrote in news:0a2d735d-066f-4f45-98b8-
    > :
    >
    >> I am having trouble with calling subroutines using a reference
    >> variable.


    Actually, you have reading trouble. You're supposed to copy-paste your
    code and *anyway* provide *exact* error messages. Maybe you'll just go
    back to killfile where you've came from?

    *SKIP*
    > Funny, it gives me:
    >
    > C:\Temp> ttt
    > Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
    > (Missing operator before ?)
    > syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
    > \ttt.pl aborted due to compilation errors.
    >


    Forget him. Look what I've found.

    {62446:61} [0:0]$ perl -MO=Deparse -wle ' { $x ? $y : $z } @A '
    Useless use of a variable in void context at -e line 1.
    Useless use of a variable in void context at -e line 1.
    Useless use of a variable in void context at -e line 1.
    Name "main::y" used only once: possible typo at -e line 1.
    Name "main::z" used only once: possible typo at -e line 1.
    Name "main::x" used only once: possible typo at -e line 1.
    Name "main::A" used only once: possible typo at -e line 1.
    BEGIN { $^W = 1; }
    BEGIN { $/ = "\n"; $\ = "\n"; }
    {
    $x ? $y : $z;
    }
    @A;
    -e syntax OK

    If I swap block(?) and array, then it becomes hash slice. But what is
    this?

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, Jan 22, 2009
    #9
    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. Roger Leigh
    Replies:
    8
    Views:
    473
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    484
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    641
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    760
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    911
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page