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

I am currently developing a library for a plethora of perl scripts and was wondering two seperate things, can scalars be passed by reffernence and if so would the resulting regular expression in the library untaint them?

I know my regular expression does untaint it when its just in the script itself, but reusablity is a goal for this project so I would like to put it in the library.

If scalars cannot be passed by refferance but the library can untaint could anyone show me a simple sub which takes the scalar untaints it (this is an example i dont need a working reg-ex already have that, but thank you for thinking of providing one) and returns the value some how or if the untainting failed return some sort of error that can be check for?
  • Comment on passing of values by reffernence and taint mode?

Replies are listed 'Best First'.
Re: passing of values by reffernence and taint mode?
by hardburn (Abbot) on Jun 06, 2003 at 15:09 UTC

    can scalars be passed by reffernence

    Sure can.

    resulting regular expression in the library untaint them?

    You need to dereferance the value and untaint it:

    sub untaint_ref { my $in = shift; $$in =~ /(.*)/; return $1; } my $val = 12345; $val = untaint_ref(\$val);

    Obviously, you want a better untainting method than /(.*)/.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      at this point it seems clear to me that i have no idea what i am doing.... after messing with this for the last hour the best i have gotten is

      #!/usr/bin/perl -w use strict; my $thingy = "No Change\n"; print $thingy; &change($thingy); print $thingy; sub change { my $in = shift; $$in = "Changed\n"; }
      I get the following error
      Can't use string ("No Change") as a SCALAR ref while "strict refs" in +use at asdf.pl line 12
      It seems like it is doing some sort of weird passing by value thing to me... but then I have no idea what I am doing (haven't you noticed...sigh) I read through perlref and it mentioned this problem existed but I could discern no answer also the 2 perl books I have on hand made similar statments with no answer I could find.

      and I forgot to login, blast I guess today is not my day.......

        It seems like it is doing some sort of weird passing by value thing to me

        Yes, but it's not weird, it's doing exactly what you're telling it -- passing a string that you're then trying to dereference. Look at hardburn's post and you'll see the critical difference is the backslash preceding the scalar.

        &change($thingy); # passes by value &change(\$thingy); # creates and passes a *reference*


        "The dead do not recognize context" -- Kai, Lexx

        First, you should avoid the ampersand syntax unless you have a specific reason to use it.

        Second, you're passing by value, not by referance. When you get to the $$in = "Changed\n"; part, perl tries to use $in as a symbolic ref. Symbolic refs are (rightfully) illegal with strict 'refs' turned on, hence the error message. To pass by a hard referance, you need to change how you call the sub to change(\$thingy);. Notice the '\' before the '$', which is saying "make a referance to $thingy". No change should be necessary to the subroutine itself.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: passing of values by reffernence and taint mode?
by particle (Vicar) on Jun 06, 2003 at 15:30 UTC
    1. can scalars be passed by reference?
      yes. any data type can be passed by reference, be it scalar, array, hash, glob, etc. though, usually scalars aren't passed as a reference, because they're so easy to pass around as they are.
    2. would the resulting regular expression in the library untaint them?
      if you mean the 'corresponding' regular expression, then yes. but you must first dereference the reference. for more on that, take a look at perlref.

    for more tainting info, there's a good tutorial that includes tainting at Ovid's Web Programming with Perl course

    ~Particle *accelerates*