Re: user arguments, oracle - insert, delete and drop!

2008-01-14 Thread perlmunky
On Jan 11, 2008 6:47 PM, Chris Charley [EMAIL PROTECTED] wrote:


 See this colimn by Randal L. Schwartz. It descibes injection attacks.
 http://www.stonehenge.com/merlyn/UnixReview/col58.html

 Thanks, I read the page, alongside the wikipedia page and I think I
understand what an sql injection is.  My code follows.  The first section is
for a text suggestion drop-down menu - it produces a selection based on the
current user input in a text box and runs a DBI query to get suggestions for
a user.  The details are passed by a javascript routine to catalyst.

sub _build_suggestion_list {
my ($self, $c) = @_;

my $table_and_col_name = $c-req-params-{column_name};
my $current_text   = $c-req-params-{text_input};

my @well_column_names = @{_get_column_names($c,T1)};
my @gi_column_names   = @{_get_column_names($c,T2)};

my %genuine_columns = ();
for (@gi_column_names)   { $genuine_columns{${$_}[0]} = 1; }
for (@well_column_names) { $genuine_columns{${$_}[0]} = 1; }

#I dont think I need the {1,}
if ($current_text =~ /\:{1,}|\--{1,}|\#{1,}|\;|insert|drop|create|^$/ig)
{
my $tc = ulliInvalid Input (: ; --; #)/li/ul;
$c-res-body($tc);
} else {
if ( exists $genuine_columns{$table_and_col_name}) {
my ($table, $column) = split /\./, $table_and_col_name;
my $model= $c-model(q(DB));
my $dbh  = $model-storage-dbh;

my $select = (select distinct $table\.$column from $table where
$table\.$column LIKE \'\%$current_text\%\');
my $sql_exe = $dbh-prepare($select); $sql_exe-execute();
my $results = $sql_exe-fetchall_arrayref();

my $options = ul;
for (@$results) {
$options .= li${$_}[0]/li;
}
$options .= /ul;
$c-res-body($options);
}else {
my $tc = ulliThe column names are invalid - please see
admin/li/ul;
$c-res-body($tc);
}
}
}

The next section contains the query constructor.  This takes all the user
input from multiple rows (users can create as many as they want).  I have
limited the options but this, i think, doesn't help because someone could
craft their own post and submit that. The query is run in another subroutine
which returns the data to the template toolkit end of things.

sub _query : Local {
my ( $self, $c ) = @_;

my $href_params = ($c-request-parameters());

delete $$href_params{'button'};

my @well_column_names = @{_get_column_names($c,T1)};
my @gi_column_names   = @{_get_column_names($c,T2)};

my %genuine_columns = ();
for (@gi_column_names)   { $genuine_columns{${$_}[0]} = 1; }
for (@well_column_names) { $genuine_columns{${$_}[0]} = 1; }

#Are the columns the ones that they should be?
@{$$href_params{'select_these'}} = grep { $genuine_columns{$_} }
@{$$href_params{'select_these'}};

my $select = ;
if ( ref($$href_params{'select_these'}) =~ 'ARRAY' or
ref($$href_params{'select_these'}) =~ 'REF') {
if ( @{$$href_params{'select_these'}} == 0 ) { $select =  T2.y ;}
else { $select = join( ,, @{$$href_params{'select_these'}}); }
} else {
if ($$href_params{'select_these'} eq ) { $select = T1.x; }
else {
if ( exists $genuine_columns{$$href_params{'select_these'}}) {
$select = $$href_params{'select_these'};
} else {
$select = T1.x;
}
}
}

#Two blocks - one for where there are multiple user conditions and the other
for singles
my $conditional = ;
if ( ref($$href_params{'conditional_type'}) =~ 'ARRAY' or
ref($$href_params{'conditional_type'}) =~ 'REF') {
for (my $i = 0; $i  @{$$href_params{'conditional_type'}}; $i++) {
$conditional .= ${$$href_params{'conditional_type'}}[$i]   .
' ';
$conditional .= ${$$href_params{'select_column_name'}}[$i] .
' ';

#Check for foul play
if (${$$href_params{'user_text'}}[$i] =~
/\;|:|--|#|insert|drop|create|\'|\|null/ig ) {
${$$href_params{'user_text'}}[$i] = null; #This can't
be 'null'
}

if ( ${$$href_params{'case_option'}}[$i] =~ /^like$/ig ) {
$conditional .= ${$$href_params{'case_option'}}[$i]
'\%${$$href_params{'user_text'}}[$i]\%'. ' ';
} else {
$conditional .=
${$$href_params{'case_option'}}[$i]. ' ';
$conditional .=
${$$href_params{'user_text'}}[$i]  . ' ';
}
}
} else {
if ($$href_params{'user_text'} =~
/\;|:|--|#|insert|drop|create|\'|\|null/ig ) {
$$href_params{'user_text'} = null;
}
if ($$href_params{'case_option'} =~ /like/ig) {
$$href_params{'user_text'} =
'\%$$href_params{'user_text'}\%' ;
  

Re: Strange interaction of my-variables with initialization

2008-01-14 Thread Dr.Ruud
Peter Daum schreef:

 my $x = undef;
 foreach ( qw(a b c) ) {
 my $t = $x if $x;
 warn( \$t == , $t||'undef', \n );
 $t = $_;
 }

 $t would be initialized with the value of $x if that was true;
 otherwise (at least that's what I would expect) $t should be
 undefined,
 so the result would be as before. The real outcome, however, is:

 $t == undef
 $t == a
 $t == b

 $t now retains its value from the last loop iteration.
 Is this a bug or a feature (tm)?

In a short and positive way:
The runtime-actions attached to my(), which include resetting the value,
are skipped when the condition says so.

-- 
Affijn, Ruud

Gewoon is een tijger.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Threaded chat server

2008-01-14 Thread Dr.Ruud
Turner schreef:

 I'm currently in the process of writing a chat server in Perl.
 Everything is all hunky-dory--it parses commands as it should, and is,
 of course, quite satisfying. Except for one thing, and that is that it
 cannot handle multiple clients at once, which, needless to say, is
 kind of useful for a chat program, isn't it? So I've been following
 the discussion online of Threads vs. forking vs. non-blocking IO, and
 I've decided to try threads, which is neat because this is the first
 thing I've ever done with threading. However, my excitement has been
 somewhat dampened by the fact that it does not work. It can still
 happily handle a single client--no complaints there. However, it can
 still ONLY handle a single client. There's probably a hole in my
 understanding of threads (e.g., I don't entirely understand what
 join() and detach() DO...). Below is the relevant server code, and I
 was hoping some kind soul could look at it, suppress his laughter at
 my naive code and point me in the right direction.

See also POE. http://search.cpan.org/search?query=POEmode=module

-- 
Affijn, Ruud

Gewoon is een tijger.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: user arguments, oracle - insert, delete and drop!

2008-01-14 Thread John W. Krahn

perlmunky wrote:

On Jan 11, 2008 6:47 PM, Chris Charley [EMAIL PROTECTED] wrote:


See this colimn by Randal L. Schwartz. It descibes injection attacks.
http://www.stonehenge.com/merlyn/UnixReview/col58.html

Thanks, I read the page, alongside the wikipedia page and I think I

understand what an sql injection is.  My code follows.  The first section is
for a text suggestion drop-down menu - it produces a selection based on the
current user input in a text box and runs a DBI query to get suggestions for
a user.  The details are passed by a javascript routine to catalyst.

sub _build_suggestion_list {
my ($self, $c) = @_;

my $table_and_col_name = $c-req-params-{column_name};
my $current_text   = $c-req-params-{text_input};

my @well_column_names = @{_get_column_names($c,T1)};
my @gi_column_names   = @{_get_column_names($c,T2)};

my %genuine_columns = ();
for (@gi_column_names)   { $genuine_columns{${$_}[0]} = 1; }
for (@well_column_names) { $genuine_columns{${$_}[0]} = 1; }


Why copy two the two arrays when you can just use the references 
directly?  The dereferencing syntax is usually $_-[0] not ${$_}[0]. 
You can remove a lot of the duplication by doing it like this:


my %genuine_columns = map { $_-[0] = 1 }
  map @{ _get_column_names( $c, $_ ) },
  qw/T1 T2/;



#I dont think I need the {1,}
if ($current_text =~ /\:{1,}|\--{1,}|\#{1,}|\;|insert|drop|create|^$/ig)


The characters ':', '-', '#' and ';' are not special in regular 
expressions so they don't have to be escaped.  The quantifier '{1,}' 
could be written more simply as '+'.  The pattern is evaluated in 
boolean context so the /g option is extraneous.




{
my $tc = ulliInvalid Input (: ; --; #)/li/ul;
$c-res-body($tc);
} else {
if ( exists $genuine_columns{$table_and_col_name}) {
my ($table, $column) = split /\./, $table_and_col_name;
my $model= $c-model(q(DB));
my $dbh  = $model-storage-dbh;

my $select = (select distinct $table\.$column from $table where
$table\.$column LIKE \'\%$current_text\%\');


The characters ., ' and % are not special in a double quoted 
string so they don't have to be escaped.




my $sql_exe = $dbh-prepare($select); $sql_exe-execute();
my $results = $sql_exe-fetchall_arrayref();

my $options = ul;
for (@$results) {
$options .= li${$_}[0]/li;


${$_}[0] is usually written as $_-[0].


}
$options .= /ul;
$c-res-body($options);
}else {
my $tc = ulliThe column names are invalid - please see
admin/li/ul;
$c-res-body($tc);
}
}
}

The next section contains the query constructor.  This takes all the user
input from multiple rows (users can create as many as they want).  I have
limited the options but this, i think, doesn't help because someone could
craft their own post and submit that. The query is run in another subroutine
which returns the data to the template toolkit end of things.

sub _query : Local {
my ( $self, $c ) = @_;

my $href_params = ($c-request-parameters());

delete $$href_params{'button'};


$$href_params{'button'} is usually written as $href_params-{button}


my @well_column_names = @{_get_column_names($c,T1)};
my @gi_column_names   = @{_get_column_names($c,T2)};

my %genuine_columns = ();
for (@gi_column_names)   { $genuine_columns{${$_}[0]} = 1; }
for (@well_column_names) { $genuine_columns{${$_}[0]} = 1; }


Same as with the other subroutine:

my %genuine_columns = map { $_-[0] = 1 }
  map @{ _get_column_names( $c, $_ ) },
  qw/T1 T2/;



#Are the columns the ones that they should be?
@{$$href_params{'select_these'}} = grep { $genuine_columns{$_} }
@{$$href_params{'select_these'}};


@{$$href_params{'select_these'}} is usually written as @{ 
$href_params-{ select_these } }


[ The same applies to every other instance where this occurs. ]



my $select = ;
if ( ref($$href_params{'select_these'}) =~ 'ARRAY' or
ref($$href_params{'select_these'}) =~ 'REF') {


You are using '=~' when you should be using 'eq'.

 if ( ref( $href_params-{ select_these } ) eq 'ARRAY' or ref( 
$href_params{ select_these } ) eq 'REF' ) {




if ( @{$$href_params{'select_these'}} == 0 ) { $select =  T2.y ;}
else { $select = join( ,, @{$$href_params{'select_these'}}); }
} else {
if ($$href_params{'select_these'} eq ) { $select = T1.x; }
else {
if ( exists $genuine_columns{$$href_params{'select_these'}}) {
$select = $$href_params{'select_these'};
} else {
$select = T1.x;
}
}
}

#Two blocks - one for where there are multiple user conditions and the other
for singles
my $conditional = 

How to get error?

2008-01-14 Thread [EMAIL PROTECTED]
Hello.
I have cgi script and sometimes it fails. I get Internal Server Error
in my browser.
When i run perl -c script.cgi  i get syntax ok.
It is runtime error. But how to know what is the error? How can i make
the error is shown in bworser window? Or where are error logs stored?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: How to get error?

2008-01-14 Thread Peter Scott
On Mon, 14 Jan 2008 02:48:08 -0800, [EMAIL PROTECTED] wrote:
 I have cgi script and sometimes it fails. I get Internal Server Error
 in my browser.
 When i run perl -c script.cgi  i get syntax ok.
 It is runtime error. But how to know what is the error? How can i make
 the error is shown in bworser window? 

use CGI::Carp qw(fatalsToBrowser);

-- 
Peter Scott
http://www.perlmedic.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: How to get error?

2008-01-14 Thread Ken Foskey

On Mon, 2008-01-14 at 02:48 -0800, [EMAIL PROTECTED] wrote:
 Hello.
 I have cgi script and sometimes it fails. I get Internal Server Error
 in my browser.
 When i run perl -c script.cgi  i get syntax ok.
 It is runtime error. But how to know what is the error? How can i make
 the error is shown in bworser window? Or where are error logs stored?

I find that this is caused by the failure to put out CGI headers
properly and Apache then says that it is not formed correctly.

Search your apache log and find the actual messages.

-- 
Ken Foskey
FOSS developer


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Threaded chat server

2008-01-14 Thread Robert Leibl
Turner wrote:
 Hello Perl gurus,

 I'm currently in the process of writing a chat server in Perl.
 Everything is all hunky-dory--it parses commands as it should, and is,
 of course, quite satisfying. Except for one thing, and that is that it
 cannot handle multiple clients at once, which, needless to say, is
 kind of useful for a chat program, isn't it? So I've been following
 the discussion online of Threads vs. forking vs. non-blocking IO, and
 I've decided to try threads, which is neat because this is the first
 thing I've ever done with threading. However, my excitement has been
 somewhat dampened by the fact that it does not work. It can still
 happily handle a single client--no complaints there. However, it can
 still ONLY handle a single client. There's probably a hole in my
 understanding of threads (e.g., I don't entirely understand what
 join() and detach() DO...). Below is the relevant server code, and I
 was hoping some kind soul could look at it, suppress his laughter at
 my naive code and point me in the right direction.
   
[...]

If you just want to handle multiple clients, you may want to look at the
select() function.
(See the bottom part in the perldoc-umentation. The one with RBITS,
WBITS, ... or here
http://www.perlfect.com/articles/select.shtml)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Searching text file and printing to new file

2008-01-14 Thread PlagueMagazine
I'm a nearly absolute beginner to Perl, and a lot of the text
manipulation things confuse me. I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Threaded chat server

2008-01-14 Thread Turner
On Jan 14, 9:31 am, [EMAIL PROTECTED] (Zentara) wrote:
 On Sun, 13 Jan 2008 11:34:36 -0800 (PST), [EMAIL PROTECTED]



 (Turner) wrote:
 I'm currently in the process of writing a chat server in Perl.
 Everything is all hunky-dory--it parses commands as it should, and is,
 of course, quite satisfying. Except for one thing, and that is that it
 cannot handle multiple clients at once, which, needless to say, is
 kind of useful for a chat program, isn't it? So I've been following
 the discussion online of Threads vs. forking vs. non-blocking IO, and
 I've decided to try threads, which is neat because this is the first
 thing I've ever done with threading. However, my excitement has been
 somewhat dampened by the fact that it does not work. It can still
 happily handle a single client--no complaints there. However, it can
 still ONLY handle a single client. There's probably a hole in my
 understanding of threads (e.g., I don't entirely understand what
 join() and detach() DO...). Below is the relevant server code, and I
 was hoping some kind soul could look at it, suppress his laughter at
 my naive code and point me in the right direction.

 This code is the only threaded chat server which seems to work.
 It may show you the way.

 Seehttp://perlmonks.org/?node_id=319472

 It's tricky to use dynamically spawned threads, because each successive
 thread gets a copy of the parent. This can cause confusion in the
 IO::Select object ( and that is probably why your code handles only 1
 client), but I havn't tested it.

 Also you will need a bidirectional client to work with the above server.

 #!/usr/bin/perl -w
 use strict;
 use IO::Socket;
 # bi-directional client

 my ( $host, $port, $kidpid, $handle, $line );

 ( $host, $port ) = ('192.168.0.1',);

 #my $name = shift || '';
 #if($name eq ''){print What's your name?\n}
 #chomp ($name = );

 # create a tcp connection to the specified host and port
 $handle = IO::Socket::INET-new(
 Proto= tcp,
 PeerAddr = $host,
 PeerPort = $port
   )
   or die can't connect to port $port on $host: $!;
 $handle-autoflush(1);# so output gets there right away
 print STDERR [Connected to $host:$port]\n;

 # split the program into two processes, identical twins
 die can't fork: $! unless defined( $kidpid = fork() );

 # the if{} block runs only in the parent process
 if ($kidpid) {

 # copy the socket to standard output
 while ( defined( $line = $handle ) ) {
   print STDOUT $line;
   }
 kill( TERM, $kidpid );# send SIGTERM to child

 }

 # the else{} block runs only in the child process
 else {

 # copy standard input to the socket
 while ( defined( $line = STDIN ) ) {
 #print $handle $name-$line;
 print $handle $line;
 }}

 __END__

 zentara

 --
 I'm not really a human, but I play one on earth.http://zentara.net/japh.html

So what was my problem in the above code? Does thread creation block
in some way? Why didn't my loop spawn off a separate thread for each
incoming connection? If each gets a copy of the parent, as you said,
why is it that it only seems to read one socket (the first client)?


Thanks,
Turner


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Searching text file and printing to new file

2008-01-14 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

I'm a nearly absolute beginner to Perl,


Then this site ought to be useful: http://learn.perl.org/


and a lot of the text manipulation things confuse me.


Really? Which things specifically, and in what way?


I have a large text file with
information essentially broken into lines like this:

findable text with a regexp
information I care about
more findable text

There are plenty of sections like this in the file. How can I write a
program that opens the file then searches for the middle line and
prints it to a new file?


What have you tried so far?

Example:

open my $IN,  '', 'infile.txt' or die $!;
open my $OUT, '', 'outfile.txt' or die $!
while ( $IN ) {
print $OUT scalar $IN if /^findable/;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




references to complex data structures

2008-01-14 Thread Kevin Viel

Consider the following anonymous array of anonymous arrays:

my $a = [ [ 00 , 01 ]
, [ 10 , 11 ]
] ;

I can print the first element of the first anonymous array:

print $$a[ 0 ][ 0 ]\n ;

or, equivalently, I can explicit use bracers to dereference it:

print ${$a}[ 0 ][ 0 ]\n ;


Should I not need two pairs of bracers?  $a is a reference to the anonymous
array.  The elements of the anonymous arrays are references to anonymous
arrays, correct?

The following seems to achieve this result:

print ${${$a}[ 0 ]}[ 0 ]\n ;

Is the outmost pair of bracers with the appropriate symbol ($, @, %) the
default?  If so, how does perl select the correct symbol?



I realize that it is seemingly moot, but it may help my understanding of
more complex structures, like hash of arrays or hash of hases.




Thanks,

Kevin

Kevin Viel, PhD
Post-doctoral fellow
Department of Genetics
Southwest Foundation for Biomedical Research
San Antonio, TX 78227 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Adding a hash to an existing hash?

2008-01-14 Thread philzenbowl
This is the bit of code I'm attempting to use,
$workPackages{ $tempHash{ WORK_PACKAGE } }
{ $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };  I
get the following error, Can't use string (1) as a HASH ref while
strict refs 

Is this because the value stored needs to be the reference to the new
hash?  If so, how do I do this?  Do I need to reset the original hash
value from 1 to null?

Thanks for any help.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Adding a hash to an existing hash?

2008-01-14 Thread Gunnar Hjalmarsson

[EMAIL PROTECTED] wrote:

This is the bit of code I'm attempting to use,
$workPackages{ $tempHash{ WORK_PACKAGE } }
{ $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };  I
get the following error, Can't use string (1) as a HASH ref while
strict refs 


For me, that piece of code results in a syntax error.

It's also unclear to me what it is you are trying to achieve.


Is this because the value stored needs to be the reference to the new
hash?


Well, maybe, a hash value cannot be a hash, but it can be a hash reference.


If so, how do I do this?


Again, what's this? I don't see from the code you posted any attempt 
to assign a hash to another hash.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: references to complex data structures

2008-01-14 Thread Gunnar Hjalmarsson

Kevin Viel wrote:

Consider the following anonymous array of anonymous arrays:

my $a = [ [ 00 , 01 ]
, [ 10 , 11 ]
] ;

I can print the first element of the first anonymous array:

print $$a[ 0 ][ 0 ]\n ;

or, equivalently, I can explicit use bracers to dereference it:

print ${$a}[ 0 ][ 0 ]\n ;

Should I not need two pairs of bracers?  $a is a reference to the anonymous
array.  The elements of the anonymous arrays are references to anonymous
arrays, correct?

The following seems to achieve this result:

print ${${$a}[ 0 ]}[ 0 ]\n ;


Well, TIMTOWDI. :) I would have said:

print $a-[0][0]\n;

Have you studied the applicable Perl docs?

perldoc perllol
perldoc perldsc
perldoc perlref
perldoc perlreftut

They explain it better than I ever would be able to...

If you seek help here to better understand references, you'd better do 
so via specific questions on one or more of those documents.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Adding a hash to an existing hash?

2008-01-14 Thread Rob Dixon

[EMAIL PROTECTED] wrote:


This is the bit of code I'm attempting to use,
$workPackages{ $tempHash{ WORK_PACKAGE } }
{ $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };  I
get the following error, Can't use string (1) as a HASH ref while
strict refs 

Is this because the value stored needs to be the reference to the new
hash?  If so, how do I do this?  Do I need to reset the original hash
value from 1 to null?


You're assigning the value of

  $tempHash{TECHNICAL_DIRECTIVE}

to

  $workPackages{$tempHash{WORK_PACKAGE}}-{$tempHash{USE_ON_CODE}}

and the error is because

  $workPackages{$tempHash{WORK_PACKAGE}}

has the value 1 instead of being a hash reference.

There's little here to help me guess what has gone wrong. Can you tell
us more about your data structures and how they have been built please?

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: references to complex data structures

2008-01-14 Thread Rob Dixon

Kevin Viel wrote:

Consider the following anonymous array of anonymous arrays:

my $a = [ [ 00 , 01 ]
, [ 10 , 11 ]
] ;

I can print the first element of the first anonymous array:

print $$a[ 0 ][ 0 ]\n ;

or, equivalently, I can explicit use bracers to dereference it:

print ${$a}[ 0 ][ 0 ]\n ;


OK

But they're 'braces' :)


Should I not need two pairs of bracers?


The 'missing' pair of braces is because you're using both of the two
different syntaxes for indexing a referenced array. The first element of
the outer array is either

  ${$a}[0]  (which can be written $$a[0] without ambiguity)

or

  $a-[0]

These are references to the first inner array, which can also be indexed
in either way. So we have four ways of reaching the first element of the
first inner array:

  ${${$a}[0]}[0]

  ${$a}[0]-[0]

  ${$a-[0]}[0]

  $a-[0]-[0]

In addition, Perl allows us to remove the arrow operator between pairs
of closing and opening brackets or braces - ][ or }{, so the second and
last options can be written

  ${$a}[0][0]

  $a-[0][0]

and so your code turns out to be the same as the first of these two. If
you use the same syntax for indexing in both cases then the second pair
of braces reappears. It is also the same as your final example below.


$a is a reference to the anonymous array.  The elements of the

 anonymous arrays are references to anonymous arrays, correct?

They are references to arrays, yes. They may be named arrays or
anonymous ones.


The following seems to achieve this result:

print ${${$a}[ 0 ]}[ 0 ]\n ;



Is the outmost pair of bracers with the appropriate symbol ($, @, %) the
default?  If so, how does perl select the correct symbol?


I'm not sure what you mean here: you have explicit dollar signs in this
code for both dereferences. Perl has no default way of handling
references, but it will complain if you try to use a reference to one
type of data as something different.


I realize that it is seemingly moot, but it may help my understanding of
more complex structures, like hash of arrays or hash of hases.


Don't get to like them to the extent that you use them when your data
isn't shaped that way. They are a way of expressing hierarchical data
only.

HTH,

Rob



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Usage of fonts...

2008-01-14 Thread Richie
Hi all,

I'm new to Perl.
Currently, I have a report that gets generated using format function
which gets output to a file.
Is it possible to be able to change the font or maybe convert the
output file to a Word document?

Many thanks!

Richard


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Threaded chat server

2008-01-14 Thread Turner
On Jan 14, 4:07 pm, [EMAIL PROTECTED] (Robert Leibl) wrote:
 Turner wrote:
  Hello Perl gurus,

  I'm currently in the process of writing a chat server in Perl.
  Everything is all hunky-dory--it parses commands as it should, and is,
  of course, quite satisfying. Except for one thing, and that is that it
  cannot handle multiple clients at once, which, needless to say, is
  kind of useful for a chat program, isn't it? So I've been following
  the discussion online of Threads vs. forking vs. non-blocking IO, and
  I've decided to try threads, which is neat because this is the first
  thing I've ever done with threading. However, my excitement has been
  somewhat dampened by the fact that it does not work. It can still
  happily handle a single client--no complaints there. However, it can
  still ONLY handle a single client. There's probably a hole in my
  understanding of threads (e.g., I don't entirely understand what
  join() and detach() DO...). Below is the relevant server code, and I
  was hoping some kind soul could look at it, suppress his laughter at
  my naive code and point me in the right direction.

 [...]

 If you just want to handle multiple clients, you may want to look at the
 select() function.
 (See the bottom part in the perldoc-umentation. The one with RBITS,
 WBITS, ... or herehttp://www.perlfect.com/articles/select.shtml)

Thank you, that was exactly what I needed. It works beautifully now.
Thanks.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




how to take action based on the existence of a value in a hash

2008-01-14 Thread Angus Glanville

Hi,

I have a small hash of directory values.  When a user inputs a  
directory value I want to check to see if this directory value is in  
my hash.  If it is not the requested function will be denied.  My  
problem is that when I iterate over the hash I get answers for every  
key in the hash.  so in my example below if I enter /usr/bin I will  
get a match found and no match found.  What I want is to only get  
a no match found error if the value is never found in the hash.  I  
tried using exists but this seems to check for the existence of a  
key not a value, and the user will input the value.


thanks in advance for any hints.

-Angus


#!/usr/bin/perl
use strict;
use warnings;

print enter path:;
chomp (my $dir = STDIN);

my %directory_hash = (dir1 = /usr/bin,
  dir2 = /users/home,);

foreach my $value ( values %directory_hash) {
unless ($value eq $dir) {
print no match found!\n;
last;
}
print match found\n
}