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

K

krystian.wojtas

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
 
P

Peter J. Holzer

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
 
R

Rainer Weikusat

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

krystian.wojtas

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top