Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to force list context on passed args to built-in pack function?

Reply
Thread Tools

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

 
 
krystian.wojtas@gmail.com
Guest
Posts: n/a
 
      08-16-2013
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
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-16-2013
On 2013-08-16 10:45, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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
| | | (E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-16-2013
(E-Mail Removed) 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.

 
Reply With Quote
 
krystian.wojtas@gmail.com
Guest
Posts: n/a
 
      08-16-2013
> 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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-16-2013
On 2013-08-16 11:43, (E-Mail Removed) <(E-Mail Removed)> 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
| | | (E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get JSON values and how to trace sessions?? webmaster@terradon.nl Python 2 04-25-2013 02:12 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
args v. *args passed to: os.path.join() Pierre Fortin Python 2 09-18-2004 06:59 PM
When passing functions as args,how to pass extra args for passed function? python@sarcastic-horse.com Python 3 09-17-2003 12:25 AM



Advertisments