How to force list context on passed args to built-in pack function?

Discussion in 'Perl Misc' started by krystian.wojtas@gmail.com, Aug 16, 2013.

  1. Guest

    Hi

    The program I'm working on is creating a database using the built-in pack function. It needs to add a feature which makes some statistics about data usage by particular arrays.

    The best approach I think is to use the Aspect module and insert some additional code after invoking the pack method. But as far as I see, hijacking the built-in function is not working. Could you confirm this?

    In this case I prepared a wrapper function which is like this:

    sub pack_wrapper { pack(@_); }

    But it doesn't work. It looks like the pack function forces the list @_ to be passed in scalar context. In this case, pack gets the size of the array instead of its content.

    The pack function hasn't a fixed number of arguments, so I cannot pass it directly like this:

    sub pack_wrapper { pack($_[0], $_[1]); }

    Probably I can use meta-programming to generate the code which invokes pack with all arguments explicitly depending on the list size. But such a workaround feels really awful for me.

    Is it possible to pass my list @_ in list context to the pack function in some perl clean way?

    Thanks for your answer and sorry if my question is obvious for you, I have already spent some time and cannot find the right solution.

    Best regards,
    Krystian
    , Aug 16, 2013
    #1
    1. Advertising

  2. Re: How to force list context on passed args to built-in packfunction?

    On 2013-08-16 10:45, <> wrote:
    > In this case I prepared a wrapper function which is like this:
    >
    > sub pack_wrapper { pack(@_); }
    >
    > But it doesn't work. It looks like the pack function forces the list
    > @_ to be passed in scalar context.


    Yes. pack expects a scalar and a list.

    > In this case, pack gets the size of
    > the array instead of its content.
    >
    > The pack function hasn't a fixed number of arguments, so I cannot pass
    > it directly like this:
    >
    > sub pack_wrapper { pack($_[0], $_[1]); }


    You can, however, do pass the rest of the arguments like this:

    sub pack_wrapper { pack($_[0], @_[1..$#_]); }

    However, I would prefer the more readable variant

    sub pack_wrapper {
    my ($template, @list) = @_;
    pack($template, @list);
    }

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaƟt. -- Ralph Babel
    Peter J. Holzer, Aug 16, 2013
    #2
    1. Advertising

  3. writes:
    > The program I'm working on is creating a database using the built-in
    > pack function. It needs to add a feature which makes some statistics
    > about data usage by particular arrays.
    >
    > The best approach I think is to use the Aspect module and insert
    > some additional code after invoking the pack method. But as far as I
    > see, hijacking the built-in function is not working. Could you
    > confirm this?
    >
    > In this case I prepared a wrapper function which is like this:
    >
    > sub pack_wrapper { pack(@_); }
    >
    > But it doesn't work.


    That's because of the pack prototype. You could use

    sub pack_wrapper { pack(shift, @_); }

    instead.
    Rainer Weikusat, Aug 16, 2013
    #3
  4. Guest

    > That's because of the pack prototype. You could use
    >
    >
    >
    > sub pack_wrapper { pack(shift, @_); }
    >
    >
    >
    > instead.


    Thank you very much for your answer, it works perfectly now :)
    , Aug 16, 2013
    #4
  5. Re: How to force list context on passed args to built-in packfunction?

    On 2013-08-16 11:43, <> wrote:
    >> That's because of the pack prototype. You could use
    >>
    >> sub pack_wrapper { pack(shift, @_); }
    >>
    >> instead.

    >
    > Thank you very much for your answer, it works perfectly now :)


    Do you understand why it works?

    Will you still understand it in 6 months?

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaƟt. -- Ralph Babel
    Peter J. Holzer, Aug 16, 2013
    #5
    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. Anand
    Replies:
    2
    Views:
    894
    Anand
    Sep 11, 2003
  2. Replies:
    3
    Views:
    484
    David Eppstein
    Sep 17, 2003
  3. Tim Jones
    Replies:
    0
    Views:
    371
    Tim Jones
    Jan 31, 2004
  4. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    674
    Pierre Fortin
    Sep 18, 2004
  5. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    216
    Charles DeRykus
    Jun 28, 2006
Loading...

Share This Page