View Full Version : Resolved groan: changing array separation.

05-23-2009, 06:16 PM

I must be suffering memory drop or maybe I need a ram upgrade. :(

I am calling in data from a sub and using it to build an array. when it comes in from the sub I need to surround it with single quotes ( ' ) and make the separator a ',' like this


I thought what I have tried would do it but I am mis recalling the correct way and I can't find where I used it before. I mayu have deleted it or lost it in a (unwelcome) windows re-install.

here's the main code:

my $returned_data = get_approved_urls();
my @valid = @$returned_data;
my $tempvar = join(" ',' ",@valid);
my @var_now = @$tempvar;
@var_now = " {'@var_now'} ";

(I tried with and without brackets)
where am I getting it wrong?


05-23-2009, 07:13 PM
See if this helps.

my $returned_data = [ 'one', 'two', 'three', 'four' ];

s/^|$/'/g for @$returned_data;

print join(',', @$returned_data);

05-23-2009, 11:07 PM
There is no way to quote an array, when you do it becomes a string. Personally I think you are confused about something but I don't know what.

05-24-2009, 03:40 AM
Thanks for the replies.

I shall ponder it while I try something else.

@Kevin: the array is currently declared in the script as

my @valid = ( 'example.com','example2.com');

I am trying to change the source of the array, to be my db.

I'll think it through some more.


05-24-2009, 04:00 AM
I am trying to change the source of the array, to be my db.

I'm not sure what you mean by that, but if you want to give us more details, I'm sure we can show you what you need to do.

05-24-2009, 05:14 AM
Thanks FishMonger.

Curently my script has the array declared within it and it get the values for it, from a flat file. No longer need to do it that way since I moved over to a mysql db :)

So I want to get the data from the db and build the array automatically.

heres the full script.

#!/usr/bin/perl -w

use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use warnings;
use DBI;
use strict;
my $cgi = new CGI;
my $referer = $ENV{'HTTP_REFERER'};
my $client = param('client');
my $img = param('img');
my $size = param('size');
my $connect = connect_to_bookings_db();

print $cgi->header;

my $valid = find_authorisedURLS();
s/^|$/'/g for @$valid;

my @valid = join(',', @$valid);

my $image_path = '/var/www/vhosts/domain/subdomains/cms/httpdocs/directory/';

my $path_to_img = $image_path . '/' . $client. '/' . $size . '/'. $img;

my $f;
print "Content-type: image/jpg\n\n";
open IMG, $path_to_img;
while (read IMG, $f, 16384) {
print $f;
close IMG;

sub check_url_referer {
my $good_ref;
my $referral_cnt = @valid;
if ($referral_cnt > 0) {

foreach my $referer (@valid) {
if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
$good_ref = "yes";
if ($good_ref ne "yes") {

sub go_away {
print qq( unauthorised url );

sub find_authorisedURLS {

my $sth = $connect->prepare ("SELECT distinct
from contact c
where c.web_url != 'null'
and c.web_url != ''
and c.web_url != ' '
and c.web_url != 'NULL'

") or die "prepare statement failed: $DBI::errstr\n";

my @returned_fields;
my $tempvar;
my @var_to_return;
my ($domain, $stuff);
my @valid;
$sth->execute() or die "execute statement failed : $DBI::errstr\n";

while ( my(@fields) = $sth->fetchrow_array() )
if ($fields[0] =~/[\w\d]+/i && $fields[0] ne '')
$fields[0] =~ s/http\:\/\///; # strip http
$fields[0] =~ s/www\.//;
push (@valid, $fields[0]);

return @valid;


sub connect_to_bookings_db {

my $db = '*******';
my $srv = '********';
my $user = '*******';
my $pass = '**********';
my $port = '****';
#$user = shift;
my $dbh = DBI->connect("DBI:mysql:$db:$srv", $user, $pass,
{'RaiseError' => 1, 'PrintError' => 0 })
or die "Connection Failed: $db DB on $srv\n\t$DBI::errstr\n";
return $dbh;

The error is

Can't use string ("73") as an ARRAY ref while "strict refs" in use at mask.pl line 22.

This is an update to an older script which is to stop hotlinking of images and to hide where they are stored.

The array works if it is :

my @valid = ('domain.com','domainTwo.com');


05-24-2009, 05:26 AM
By the way :p that now working. when I added print $cgi->header; it all became much clearer. :rolleyes:

Thanks very much for your time.


05-24-2009, 04:41 PM
A couple comments/suggestions.

Don't use the -w switch, unless it's a 1 liner command. The warnings pragma that you're already using is better.

Why load the CGI ':standard' functions when you plan on and actually use the OO methods?

Don't use the indirect object when creating objects.
Meaning, instead of this:
my $cgi = new CGI;

do this:
my $cgi = CGI->new;

my @valid = join(',', @$valid); creates a single element array, so why not just put it in a scalar?

ALWAYS check the return code of an open call and it's preferable to to use a lexical var for the filehandle as well as the 3 arg form of open.

Within a sub you almost never want to use a global var directly (I'm referring to @array). Instead, you should pass it to the sub either by reference or by value depending on what you need to do with its values.

Don't use the '&' when calling a sub unless you know, understand, and want its side effects.

Since your select statement is only retrieving 1 field, it would be cleaner to put that into a scalar instead of a 1 element array.


Hmm, I meant to only point out 1 or 2 items, did I go overboard? :o

05-24-2009, 08:23 PM
Much appreciated FishMonger.

I have to review the code anyway because usually (always), I can make it more efficient somewhere, once I get it to work.

Any shortcomings are better mentioned. :thumbsup: