in reply to why is this an uninitialized value?

$output =~ /"access_token":"(.*)", "ex/g;

You never check if the output really matches.

Most likely, if you check, then you find that it doesn't match:

$output =~ /"access_token":"(.*)", "ex/g or die "Couldn't find access token in [[$output]]";

But maybe you really do want to parse the output in a better way, since it seems as if the output could be JSON:

use JSON 'decode_json'; # or JSON::XS , or JSON::Tiny ... sub getToken { my $output = `curl -k -s -XPOST (URL) -dgrant_type=password -dclie +nt_id=(client ID) -dclient_secret=(client secret) -dusername=(usernam +e) -dpassword=(password)`; my $decoded_output = decode_json( $output ); die "Couldn't find access token in [[$output]]" unless $decoded_output->{access_token}; return $output->{access_token} }