Newbie doesn"t quite understand pointers with hashes.

Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 05:00:29 von keersarge

Help a newbie moving way too far out in left field, given my current
knowledge of Perl - but it is fun, anyway.

(Perl 5.8.8 on Debian Etch.)

I am building an object to build some HTML forms. My routine reads in a
config file that has the following line.

....

....

I want to replace the hash key with the actual value. The Perl script
reads the file in line by line is parsed out on the spaces. The following
is not really the program, just a boiled down test script that I am using
to try to understand what is going on.

....
$z = ; #read a line
@parsearray = split(' ', $z); #break it apart at the spaces
$parsearray[3] =~ s/value=//; #get rid of all but the hash key
....

Now $parsearray[3] holds the string $submithash{Address}.

I can't get from the string representation of the hash key to the actual
value (although I have discovered a ton of interesting stuff while
trying).

If I print $parsearray[3], I get the string $submithash{Address}.

print \$parsearray[3] gives me a SCALAR pointer.

print \%parsearray[3] blows up on a use strict error.

Anybody?

Thanks
Keersarge

(Late of Delphi, which is the only thing I regret leaving behind when I
left Windows).

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 05:51:23 von jurgenex

keersarge wrote:
> > size="40"> ...
>
> I want to replace the hash key with the actual value.

I think you got the terminology wrong. Assuming
$submithash{Address}
was code (it is not, it is data) and there was a hash %submithash then the
hash key would be 'Address'.

[code snipped]
> $parsearray[3] =~ s/value=//; #get rid of all but the hash key
>
> Now $parsearray[3] holds the string $submithash{Address}.

I.e. the content of $parsearray[3] is data, just as it is supposed to be.

> I can't get from the string representation of the hash key to the
> actual value (although I have discovered a ton of interesting stuff
> while trying).
>
> If I print $parsearray[3], I get the string $submithash{Address}.

That is the data that is stored in the fourth element of the array
$parsearray

> print \$parsearray[3] gives me a SCALAR pointer.

And that is the address to that fourth element.

It is not totally clear what you are trying to do but it appears as if you
are trying to execute data as if it were program code. The proper tool for
that is eval(), details see the man page.
However be warned that many people consider eval() quite dangerous and
rightfully so because unless used very carefully it opens a huge barn door
to code injection attacks.

jue

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 06:00:51 von Uri Guttman

>>>>> "JE" == Jürgen Exner writes:


JE> It is not totally clear what you are trying to do but it appears
JE> as if you are trying to execute data as if it were program
JE> code. The proper tool for that is eval(), details see the man
JE> page. However be warned that many people consider eval() quite
JE> dangerous and rightfully so because unless used very carefully it
JE> opens a huge barn door to code injection attacks.

what the OP really needs is a proper templating system. string eval is a
poor man's version of that and as you said, dangerous. also it is
overkill for this. of course i will recommend Template::Simple since it
can do this sort of templating with little effort on the part of the OP.

classify this as a classic XY problem as they needed a templater but
thought that some form of wacky perl interpolation would work.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 11:27:01 von RedGrittyBrick

keersarge wrote:
> Help a newbie moving way too far out in left field, given my current
> knowledge of Perl - but it is fun, anyway.
>
> (Perl 5.8.8 on Debian Etch.)
>
> I am building an object to build some HTML forms. My routine reads in a
> config file that has the following line.
>
> ...
>
> ...
>
> I want to replace the hash key with the actual value. The Perl script
> reads the file in line by line is parsed out on the spaces. The following
> is not really the program, just a boiled down test script that I am using
> to try to understand what is going on.
>
> ...
> $z = ; #read a line
> @parsearray = split(' ', $z); #break it apart at the spaces
> $parsearray[3] =~ s/value=//; #get rid of all but the hash key
> ...
>
> Now $parsearray[3] holds the string $submithash{Address}.
>
> I can't get from the string representation of the hash key to the actual
> value (although I have discovered a ton of interesting stuff while
> trying).
>
> If I print $parsearray[3], I get the string $submithash{Address}.
>
> print \$parsearray[3] gives me a SCALAR pointer.
>
> print \%parsearray[3] blows up on a use strict error.
>
> Anybody?

As Uri and Jürgen said, you seem to be looking for eval, but that's
often very insecure. A templating system would be better - have a look
in CPAN.

For a one-off I'd do something like
$z =~ s/\$submithash{Address}/$submithash{Address}/;

You should be able to capture and eval:
$z =~ s/\$submithash{Address}/eval($1)/e;
Which is less typing but probably less efficient (if that matters) but
perhaps part way to a more general solution. Maybe "Address" could be
replaced with an expression that matches possible keys? Maybe \w+ if you
don't care too much.

A block of such statements might serve to document what you consider to
be expressions that can *safely* be evaluated. This is safer than simply
using eval on unknown content. I'd worry about code injection attacks.

For anything non-trivial I'd not reinvent the templating wheel :-)

> Thanks
> Keersarge
>
> (Late of Delphi, which is the only thing I regret leaving behind when I
> left Windows).

I greatly enjoyed using Delphi versions 1 and 2 in the distant past. For
me, Perl replaced Turbo Pascal 3 and Java replaced Delphi.

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 14:48:08 von keersarge

Thanks for all the answers, but I don't think I got my question across
properly.

But first...

Everyone mentioned templating. When I first discovered it a few weeks ago
I thought it was a great idea to keep HTML separate from the perl CGI
stuff. It worked great with presentation of stuff using a browser. Then
as I began to get into it, I got stuck on using the

method in a
template.

More reading in the OReilley books and the official CGI.pm book. All of
them said in effect "Yes, you can use the FORM method with templates,
but...
We don't recommend it.
It is a can of worms.
The form logic will overwhelm your mainline code.
Don't use it... and so forth."

At that point I said, WTH! This is like purchasing a brand new car and
then reading in the owners manual that "We don't recommend driving on the
Interstate highways because the wheels might fall off."

By the way,my code is on a private internal network and cannot be accessed
from the 'Net.

At that point I went back to looking for another method to separate HTML
and CGI.

Now let me try my original question again.

I have a long string in some file somewhere that has what looks like a
reference to a hash element in it. Like so...

asfp poip wwdudjh skkienb $submithash(somekey) llsigj kosoone

By coincidence I also have a program that has a hash by that same name
i.e. %submithash and it has that same key somewhere in it, say,
$submithash(somekey)= 5986

I want to regex the reference to $submithash(somekey) out of that file (no
problem with that) and, inside my program, actually use it and get the
value out of $submithash(somekey).

What I got was something like $mystring = $submithash(somekey). If I
print $mystring, I get the literal string, $submithash(somekey), not the
value that is 5986. I need to turn that string into the actual reference
to the hash.

Thanks
Keersarge

(Late of Delphi, which is the only thing I regret leaving behind when I
left Windows).

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 15:24:23 von jurgenex

Keersarge wrote:
> I have a long string in some file somewhere that has what looks like a
> reference to a hash element in it. Like so...
>
> asfp poip wwdudjh skkienb $submithash(somekey) llsigj kosoone
>
> By coincidence I also have a program that has a hash by that same name
> i.e. %submithash and it has that same key somewhere in it, say,
> $submithash(somekey)= 5986
>
> What I got was something like $mystring = $submithash(somekey). If I
> print $mystring, I get the literal string, $submithash(somekey), not
> the value that is 5986. I need to turn that string into the actual
> reference to the hash.

Well, if you insist on shooting yourself in the foot then as I told you
before eval() is the function that will execute data as if it were code.

jue

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 15:31:54 von spambait

In article , "Jürgen Exner" wrote:
>Keersarge wrote:
>> What I got was something like $mystring = $submithash(somekey). If I
>> print $mystring, I get the literal string, $submithash(somekey), not
>> the value that is 5986. I need to turn that string into the actual
>> reference to the hash.
>
>Well, if you insist on shooting yourself in the foot then as I told you
>before eval() is the function that will execute data as if it were code.

Well, he did say

"By the way,my code is on a private internal network and cannot be accessed
from the 'Net."

which suggests that the risk of shooting himself in the foot is minimal.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 15:56:00 von jurgenex

Doug Miller wrote:
> "By the way,my code is on a private internal network and cannot be
> accessed from the 'Net."
>
> which suggests that the risk of shooting himself in the foot is
> minimal.

Had you said 'lower' instead of 'minimal', then I would have agreed.

But it is wrong to assume that just because you are on a corporate network
nobody will try to crack your application from within. Think disgruntled
employee or career weasel or other similarly unpleasant characters. And we
don't know what the OPs system is for. Maybe corporate financial data or
confidential employee data?

Or think about what may happen if someone from outside does gain access to
the internal network and now finds all doors wide open.

jue

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 15:59:08 von Scott Bryce

Keersarge wrote:
> Everyone mentioned templating. When I first discovered it a few weeks ago
> I thought it was a great idea to keep HTML separate from the perl CGI
> stuff. It worked great with presentation of stuff using a browser. Then
> as I began to get into it, I got stuck on using the method in a
> template.
>
> More reading in the OReilley books and the official CGI.pm book. All of
> them said in effect "Yes, you can use the FORM method with templates,
> but...
> We don't recommend it.
> It is a can of worms.
> The form logic will overwhelm your mainline code.
> Don't use it... and so forth."

I don't understand why. I use forms inside of templates with
HTML::Template, and I don't have any problems. There's a little more to
it than with ordinary text, but I wouldn't describe it as "a can or worms."

My suggestion: give it a shot, then ask here if you can't get it to
work. I would use templates over the method you are attempting.

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 23:37:33 von keersarge

> I don't understand why. I use forms inside of templates with
> HTML::Template, and I don't have any problems. There's a little more to
> it than with ordinary text, but I wouldn't describe it as "a can or worms."
>
> My suggestion: give it a shot, then ask here if you can't get it to
> work. I would use templates over the method you are attempting.

Ok. That is what I wanted to hear. When I first learned of templates, I
thought they were a great idea. Especially since my first attempts at
learning CGI were horrendous mishmashes of perl/html code that worked
pretty good as long as I was still actively building it. But going back
to it after a session on something else was a nightmare of trying to
remember what that particular routine was for.

As I said, my usually useful books either discourage forms in templates or
just flat don't talk about it at all so I will start looking for some
other place of info tomorrow at work. (My rural home dialup 24k phone
line does not make for fast surfing, alas)

Suggestions are appreciated.

Thanks a bunch

keersarge

Re: Newbie doesn"t quite understand pointers with hashes.

am 08.11.2007 23:56:09 von keersarge

>
> Or think about what may happen if someone from outside does gain access to
> the internal network and now finds all doors wide open.
>
> jue

Yes, someone could plant a wireless hub on a loose ethernet cable
somewhere, or even just the simple hack of plugging in a thumb drive. But
in this case it would get them nothing, unless they just want to commit
sabotage. Nothing on this network has any value to anyone -
no customer accounts, credit cards, financials, or even games. The odds
are extremely long that a hacker would even know what the data was about,
unless he has a control manual for an Urban 4020 2 point frame welder.

I know what network risk is. I have an active Snort monitor on the admin
network and am continually amazed that we can even get these messages
squeezed between the 'bots and the spam - especially since our company
domain name suggests a game developer. (we aren't)

But, back to the original post. I am not a Perl expert by a long shot,
or even a person very comfortable with the language yet, but I am far
enough along to realise that there is almost nothing that can't be done in
Perl. That is not to say that I know how to do most of it yet, but I am
willing to bet that there are at least a half dozen non-eval ways to
accomplish what I posted.

Thanks all
keersarge

Re: Newbie doesn"t quite understand pointers with hashes.

am 09.11.2007 00:06:47 von glex_no-spam

keersarge wrote:
[...]
> As I said, my usually useful books either discourage forms in templates or

I find that hard to believe.

A form element is nothing special and outside of some mark-up
elements/syntax a template is nothing special either, it doesn't
have to relate to HTML at all.

> just flat don't talk about it at all.

Why should a book about learning a language discuss templates?

Does it discuss using Class::DBI, Catalyst, and XML::Simple?

There are many different modules for doing templates, quite a
few of them have books covering, in detail, how to use the
particular module. There is also a lot of documentation for
most of them, all available online and once you download the
module the POD documentation, it will be available to you
without having to use the Internet.

Re: Newbie doesn"t quite understand pointers with hashes.

am 09.11.2007 00:34:20 von keersarge

On Thu, 08 Nov 2007 17:06:47 -0600, J. Gleixner wrote:

> keersarge wrote:
> [...]
>> As I said, my usually useful books either discourage forms in templates or
>
> I find that hard to believe.
>

From the FAQ from the guy who wrote HTML::Template

....It is certainly possible to produce a