perl

How to add email address to string in Perl? ( "@" character )


I have this following Perl code in the CloudBees online devops platform:

my $formattedUserEmail = $[UserEmail];
    
$additionalTestParameters .= "ECJOBLink,$jobLink;ECJOBId,$[jobId];EcJobCounter,$jobCounter,PFUser,$formattedUserEmail";

It's deployed as the following Perl code:

my $formattedUserEmail = dave.adam@site.com; 
    
$additionalTestParameters .= "ECJOBLink,$jobLink;ECJOBId,1111;EcJobCounter,$jobCounter,PFUser,$formattedUserEmail";

But when the code runs, it results in errors, i believe because of the dave.adam@site.com email address, specifically the @ character, which is a special symbol in Perl.

Array found where operator expected at ... line 32, at end of line
syntax error at .... line 32, near "adam@site"
Global symbol "@site" requires explicit package name at C.... line 32.
Execution of ..... aborted due to compilation errors.

How can I fix my code so that the email var gets added to the $additionalTestParameters string?


Solution

  • $[UserEmail] looks suspicious. Maybe influenced by your background.

    If it's a HASH (like Python's dict), it should be ${'UserEmail'}.

    $[UserEmail] is for a ARRAY member and UserEmail should be a numeric key.


    Always:

    use strict;
    use warnings; 
    

    in all of your scripts, it will helps for coding errors:

    Array found where operator expected at reply input line 1, near "adam@site"
    Bareword "dave" not allowed while "strict subs" in use at reply input line 1.
    syntax error at reply input line 1, near "adam@site"
    Global symbol "@site" requires explicit package name (did you forget to declare "my @site"?) at reply input line 1.
    BEGIN not safe after errors--compilation aborted at reply input line 7.