Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am getting the dreaded "Can't use string("STRING") as a HASH ref" error in this bit of code.
sub foo($) { my $css = CSS->new(); my $styles = CSS::Style::selectors(shift); ....
Help! I am relatively new to perl and don't understand what I am doing incorrectly. Thanks in advance!

Replies are listed 'Best First'.
Re: Can't use string ("STRING") as a HASH ref - Help!
by The Mad Hatter (Priest) on Mar 12, 2004 at 23:14 UTC

    Is the argument you're passing to foo a hash ref? That is, is it what CSS::Style::selectors() expects?

    CSS::Style::selectors probably expects to be called like a method (so something like CSS::Style->selectors(shift) instead). You might need to create a CSS::Style object first and then call the method selectors on that with the appropriate argument. Update: You probably shouldn't be using CSS::Style directly in the first place; it appears that all of this should be done via CSS.pm.

    In any case, read the doc and verify that $_[0] (what shift gets the value from) really contains what you want.

    Update2: Since you said you're new to Perl, here's probably what you want... I've tried to comment it well enough... read the doc and then ask if you need clarification... (Not tested, BTW, all from what the doc says.)

Re: Can't use string ("STRING") as a HASH ref - Help!
by Plankton (Vicar) on Mar 12, 2004 at 23:17 UTC
    It is hard to say ... what are you passing to foo? What line is the error occuring? You might need to provide a little more info. But my WAG is that you are calling foo like so ...
    foo($bar);
    ... when you should be calling it like this ...
    foo(\$bar);

    Plankton: 1% Evil, 99% Hot Gas.
Re: Can't use string ("STRING") as a HASH ref - Help!
by Anonymous Monk on Mar 12, 2004 at 23:56 UTC
    Thanks for the help all. I will give your example a try Mr. Hatter! And to further clarify I am passing a css file (in string format) in shift. I need to write a script that grabs a css file and formats it into xml. I'm grateful for the help and will be posting results shortly - thanks again!