Are you specifying your script in double quotes? If you are then Perl will try and interpolate the string and automatically expand out what it thinks are the Perl variables @intpos etc. Try using strict and warnings. escape any text that looks like a Perl variable that you don't want to be interpolated. the same is true for 'here document' syntax.
E.g.
my $script = <<EOF;
declare \@intpos int
declare \@intuser int
declare \@dtlastpost varchar(30)
set \@intuser = (select userid from $dbname.dbo.users where name = '$u
+ser')
set \@intpos = (select top 1 permissionid from $dbname.dbo.security wh
+ere userid = \@intuser order by permissionid desc)
set \@dtlastpost = (SELECT top 1 PostDate FROM $dbname.dbo.Messages wh
+ere userid = @intuser order by postdate desc )
EOF
In the example above I have assumed that you want to interpolate $dbname and $user.